package richbar.com.richbar.view.linear;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.common.util.LogUtil;

/**
 * Created by qyang on 2018-9-12.
 */

public class LinearView extends LinearLayout {
    private LinearLayoutAdapter adapter;
    private OnItemClickListener onItemClickListener;
    public LinearView(Context context) {
        super(context);
    }

    public LinearView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LinearView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;

    }


    public void setAdapter(LinearLayoutAdapter adapter){
        this.adapter = adapter;
        notifyDataSetChanged();
    }

    public void notifyDataSetChanged(){
        removeAllViews();
        if (adapter == null) {
            return;
        }
        LayoutParams layoutParams = new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        for (int i = 0; i < adapter.getCount(); i++) {
            LogUtil.e("PPPAPPAPA",i);
            View view = adapter.getView(adapter.getList(),adapter.getList().get(i),i);
            final int finalI = i;
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClicked(v, finalI);
                    }
                }
            });
            addView(view, i, layoutParams);
        }
    }

    /**
     *
     * 回调接口
     */
    public interface OnItemClickListener {
        void onItemClicked(View v, int position);
    }
}
